#!/usr/local/bin/perl

use strict;
use LWP::UserAgent;
use Crypt::SSLeay;
use JSON;
use HTTP::Cookies;
use HTTP::Status;
use Data::Dumper;

if ($#ARGV+1 != 3){
   die "You need to pass in http://host [AO/DO] csvfilePath I.E.: perl OligoMigration.pl http://retina:9090 DO /local/dudley/oligo.csv"
}
my ($host, $oligoType, $csvFile) = @ARGV;

my $browser = LWP::UserAgent->new;
$browser->protocols_allowed( [ 'http', 'https'] );
my $cookieFile = "addama_lwp_cookies.dat";
my $cookie_jar = HTTP::Cookies->new(
    file => $cookieFile,
  );
$cookie_jar->load($cookieFile);
$browser->cookie_jar($cookie_jar);
$browser->timeout(100);
$browser->env_proxy;
open (MYFILE, $csvFile);
my $i = 1;
my $chopped = '';
my $oligoName = '';
my $concentration = '';
my $yoPostCreate = '';
my $anno;
my $json = new JSON;
my $obj;
my $count = 0;
my $maxStrainNumber = 0;
while (<MYFILE>) {
  chomp;
  print "line $count $_\n";
  #@personal = split(/","/, $_);
  #$size = @personal;
  #$yoPostCreate = '"http://ravioli-2:8080/addama-rest/dudley-sample/path/datasource/YO/';
  $yoPostCreate = "\"" . $host . "/addama-rest/dudley-sample/path/datasource/" . $oligoType . "/";
  printf($yoPostCreate);
  processLine($_);
  $count = $count + 1;
}
close (MYFILE);
annotateYO();

sub annotateYO{
   my $postYOAnnotations = $host . '/addama-rest/dudley-sample/path/datasource/' .$oligoType .'/annotations?JSON={oligoCount:' .$count . ';oligoMaxNum:' . $maxStrainNumber .'}';
   my $countAnnoResponse = $browser->post($postYOAnnotations);
   if ($countAnnoResponse->is_success) {
      print("Posted $oligoType record count Annotations: -> $count" . " max number -> $maxStrainNumber");
   }
}

sub processLine{
  my $line = $_[0];
  my @splValues = split('","', $line);
  $i = 1;
  foreach my $val (@splValues) {
  #chop " off first value's beginning, offset of 1 to length
  if ($i == 1){
     $oligoName = substr($val, 1, (length($val)-1));
     #split strain name and capture max strain number
     my @oligoNameArray = split($oligoType, $oligoName);
     my $oligoNumber = $oligoNameArray[1];
     if ($oligoNumber > $maxStrainNumber){
        $maxStrainNumber = $oligoNumber;
     }
     #print("oligo number $oligoNumber\n");
     $yoPostCreate = $yoPostCreate . $oligoName .'/create?JSON={oligoKey:' . $oligoName;
  }elsif ($i == 2) {
     $anno = ';oligoName:"'. $val . '"';
     $yoPostCreate = $yoPostCreate . $anno;
}elsif ($i == 3) {
     $anno = ';yName:\''. $val . '\'';
     $yoPostCreate = $yoPostCreate . $anno;
}elsif ($i == 4) {
     $anno = ';notes:\''. $val . '\'';
     $yoPostCreate = $yoPostCreate . $anno;
}elsif ($i == 5) {
     $anno = ';sequence:\''. $val . '\'';
     $yoPostCreate = $yoPostCreate . $anno;
}elsif ($i == 6) {
     $anno = ';gene:\''. $val . '\'';
     $yoPostCreate = $yoPostCreate . $anno;
}elsif ($i == 7) {
     $anno = ';project:\''. $val . '\'';
     $yoPostCreate = $yoPostCreate . $anno;
}elsif ($i == 8) {
     $anno = ';modification:\''. $val . '\'';
     $yoPostCreate = $yoPostCreate . $anno;
}elsif ($i == 9) {
     $anno = ';purification:\''. $val . '\'';
     $yoPostCreate = $yoPostCreate . $anno;
}
elsif ($i == 10) {
     $val = substr($val,0,(length($val)-2));
     $anno = ';concentration:\''. $val .'\'';
     $yoPostCreate = $yoPostCreate . $anno;
     $yoPostCreate = $yoPostCreate .'}"';
     print("POST this: $yoPostCreate\n");
     my $response = $browser->post($yoPostCreate);
     if ($response->is_success) {
          my $response = $response->content;
          # eval in case response isn't legit json
          eval { $obj = $json->decode($response) };
          print("$yoPostCreate POSTED Successfully\n");
      }
      else {
          if($response->code()==401) {
            print "\nLogin is required, please run AddamaLogin.pl and try again.\n\n"
          } else {
            print ("POST FAILED");
            die $response->status_line;
          }
      }
   $yoPostCreate = '';
  }
   $i++;
 }
}

